{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Datawhale组队学习Pandas\n",
    "## 第五章 变形\n",
    "## 第五次打卡"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "![](https://ai-studio-static-online.cdn.bcebos.com/44ecd5e835d948dcac9cfafbac6411d452c197496adf42959f39d241f2a226d2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://mirror.baidu.com/pypi/simple/\n",
      "Collecting pandas==1.1.5\n",
      "\u001b[?25l  Downloading https://mirror.baidu.com/pypi/packages/fd/70/e8eee0cbddf926bf51958c7d6a86bc69167c300fa2ba8e592330a2377d1b/pandas-1.1.5-cp37-cp37m-manylinux1_x86_64.whl (9.5MB)\n",
      "\u001b[K     |████████████████████████████████| 9.5MB 12.7MB/s eta 0:00:01\n",
      "\u001b[?25hRequirement already satisfied: numpy>=1.15.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas==1.1.5) (1.16.4)\n",
      "Requirement already satisfied: python-dateutil>=2.7.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas==1.1.5) (2.8.0)\n",
      "Requirement already satisfied: pytz>=2017.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas==1.1.5) (2019.3)\n",
      "Requirement already satisfied: six>=1.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from python-dateutil>=2.7.3->pandas==1.1.5) (1.15.0)\n",
      "Installing collected packages: pandas\n",
      "  Found existing installation: pandas 0.23.4\n",
      "    Uninstalling pandas-0.23.4:\n",
      "      Successfully uninstalled pandas-0.23.4\n",
      "Successfully installed pandas-1.1.5\n"
     ]
    }
   ],
   "source": [
    "!pip install pandas==1.1.5 \r\n",
    "!unzip data/data65130/data.zip  -d data/ > /dev/null 2>&1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 一、长宽表的变形\n",
    "\n",
    "什么是长表？什么是宽表？这个概念是对于某一个特征而言的。例如：一个表中把性别存储在某一个列中，那么它就是关于性别的长表；如果把性别作为列名，列中的元素是某一其他的相关特征数值，那么这个表是关于性别的宽表。下面的两张表就分别是关于性别的长表和宽表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Gender</th>\n",
       "      <th>Height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>F</td>\n",
       "      <td>163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>F</td>\n",
       "      <td>160</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>M</td>\n",
       "      <td>175</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>M</td>\n",
       "      <td>180</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Gender  Height\n",
       "0      F     163\n",
       "1      F     160\n",
       "2      M     175\n",
       "3      M     180"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame({'Gender':['F','F','M','M'], 'Height':[163, 160, 175, 180]})\r\n",
    "#长表：性别特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Height: F</th>\n",
       "      <th>Height: M</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>163</td>\n",
       "      <td>175</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>160</td>\n",
       "      <td>180</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Height: F  Height: M\n",
       "0        163        175\n",
       "1        160        180"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame({'Height: F':[163, 160], 'Height: M':[175, 180]})\r\n",
    "#宽表：以性别区分的其他相关特征"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "显然这两张表从信息上是完全等价的，它们包含相同的身高统计数值，只是这些数值的呈现方式不同，而其呈现方式主要又与性别一列选择的布局模式有关，即到底是以$\\color{red}{long}$的状态存储还是以$\\color{red}{wide}$的状态存储。因此，`pandas`针对此类长宽表的变形操作设计了一些有关的变形函数。\n",
    "\n",
    "### 1. pivot\n",
    "\n",
    "`pivot`是一种典型的长表变宽表的函数，首先来看一个例子：下表存储了张三和李四的语文和数学分数，现在想要把语文和数学分数作为列来展示。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Name</th>\n",
       "      <th>Subject</th>\n",
       "      <th>Grade</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Math</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Math</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Class       Name  Subject  Grade\n",
       "0      1  San Zhang  Chinese     80\n",
       "1      1  San Zhang     Math     75\n",
       "2      2      Si Li  Chinese     90\n",
       "3      2      Si Li     Math     85"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'Class':[1,1,2,2],\n",
    "                   'Name':['San Zhang','San Zhang','Si Li','Si Li'],\n",
    "                   'Subject':['Chinese','Math','Chinese','Math'],\n",
    "                   'Grade':[80,75,90,85]})\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "对于一个基本的长变宽的操作而言，最重要的有三个要素，分别是变形后的行索引、需要转到列索引的列，以及这些列和行索引对应的数值，它们分别对应了`pivot`方法中的`index, columns, values`参数。新生成表的列索引是`columns`对应列的`unique`值，而新表的行索引是`index`对应列的`unique`值，而`values`对应了想要展示的数值列。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Subject</th>\n",
       "      <th>Chinese</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>San Zhang</th>\n",
       "      <td>80</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Si Li</th>\n",
       "      <td>90</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Subject    Chinese  Math\n",
       "Name                    \n",
       "San Zhang       80    75\n",
       "Si Li           90    85"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot(index='Name', columns='Subject', values='Grade')\r\n",
    "#可以看出来名字变成了行名"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "通过颜色的标记，更容易地能够理解其变形的过程：\n",
    "\n",
    "很像是将表格立体化的过程\n",
    "\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/9dd0204b34e24d3088e0560515897f89e73d1ab657e645989ba6548dc88cc456)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "利用`pivot`进行变形操作需要满足唯一性的要求，即由于在新表中的行列索引对应了唯一的`value`，因此原表中的`index`和`columns`对应两个列的行组合必须唯一。例如，现在把原表中第二行张三的数学改为语文就会报错，这是由于`Name`与`Subject`的组合中两次出现`(\"San Zhang\", \"Chinese\")`，从而最后不能够确定到底变形后应该是填写80分还是75分。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ValueError('Index contains duplicate entries, cannot reshape')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[1, 'Subject'] = 'Chinese'\n",
    "try:\n",
    "    df.pivot(index='Name', columns='Subject', values='Grade')\n",
    "except Exception as e:\n",
    "    Err_Msg = e\n",
    "Err_Msg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "`pandas`从`1.1.0`开始，`pivot`相关的三个参数允许被设置为列表，这也意味着会返回多级索引。这里构造一个相应的例子来说明如何使用：下表中六列分别为班级、姓名、测试类型（期中考试和期末考试）、科目、成绩、排名。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Name</th>\n",
       "      <th>Examination</th>\n",
       "      <th>Subject</th>\n",
       "      <th>Grade</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Mid</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>80</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Final</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>75</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Mid</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>85</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Final</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>65</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Mid</td>\n",
       "      <td>Math</td>\n",
       "      <td>90</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Final</td>\n",
       "      <td>Math</td>\n",
       "      <td>85</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Mid</td>\n",
       "      <td>Math</td>\n",
       "      <td>92</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Final</td>\n",
       "      <td>Math</td>\n",
       "      <td>88</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Class       Name Examination  Subject  Grade  rank\n",
       "0      1  San Zhang         Mid  Chinese     80    10\n",
       "1      1  San Zhang       Final  Chinese     75    15\n",
       "2      2      Si Li         Mid  Chinese     85    21\n",
       "3      2      Si Li       Final  Chinese     65    15\n",
       "4      1  San Zhang         Mid     Math     90    20\n",
       "5      1  San Zhang       Final     Math     85     7\n",
       "6      2      Si Li         Mid     Math     92     6\n",
       "7      2      Si Li       Final     Math     88     2"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'Class':[1, 1, 2, 2, 1, 1, 2, 2],\n",
    "                   'Name':['San Zhang', 'San Zhang', 'Si Li', 'Si Li',\n",
    "                              'San Zhang', 'San Zhang', 'Si Li', 'Si Li'],\n",
    "                   'Examination': ['Mid', 'Final', 'Mid', 'Final',\n",
    "                                    'Mid', 'Final', 'Mid', 'Final'],\n",
    "                   'Subject':['Chinese', 'Chinese', 'Chinese', 'Chinese',\n",
    "                                 'Math', 'Math', 'Math', 'Math'],\n",
    "                   'Grade':[80, 75, 85, 65, 90, 85, 92, 88],\n",
    "                   'rank':[10, 15, 21, 15, 20, 7, 6, 2]})\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "现在想要把测试类型和科目联合组成的四个类别（期中语文、期末语文、期中数学、期末数学）转到列索引，并且同时统计成绩和排名："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"4\" halign=\"left\">Grade</th>\n",
       "      <th colspan=\"4\" halign=\"left\">rank</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>Subject</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Chinese</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Math</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Chinese</th>\n",
       "      <th colspan=\"2\" halign=\"left\">Math</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>Examination</th>\n",
       "      <th>Mid</th>\n",
       "      <th>Final</th>\n",
       "      <th>Mid</th>\n",
       "      <th>Final</th>\n",
       "      <th>Mid</th>\n",
       "      <th>Final</th>\n",
       "      <th>Mid</th>\n",
       "      <th>Final</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Class</th>\n",
       "      <th>Name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <th>San Zhang</th>\n",
       "      <td>80</td>\n",
       "      <td>75</td>\n",
       "      <td>90</td>\n",
       "      <td>85</td>\n",
       "      <td>10</td>\n",
       "      <td>15</td>\n",
       "      <td>20</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <th>Si Li</th>\n",
       "      <td>85</td>\n",
       "      <td>65</td>\n",
       "      <td>92</td>\n",
       "      <td>88</td>\n",
       "      <td>21</td>\n",
       "      <td>15</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Grade                     rank                 \n",
       "Subject         Chinese       Math       Chinese       Math      \n",
       "Examination         Mid Final  Mid Final     Mid Final  Mid Final\n",
       "Class Name                                                       \n",
       "1     San Zhang      80    75   90    85      10    15   20     7\n",
       "2     Si Li          85    65   92    88      21    15    6     2"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivot_multi = df.pivot(index = ['Class', 'Name'],\n",
    "                       columns = ['Subject','Examination'],\n",
    "                       values = ['Grade','rank'])\n",
    "pivot_multi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "看一看官网文档`df.pivot?`\n",
    "\n",
    "首先是输入输出：`df.pivot(index=None, columns=None, values=None) -> 'DataFrame'`\n",
    "\n",
    "输入数据类型：`str or object or a list of str, optional`,没有的话就用已经存在的\n",
    "\n",
    "可能报错的地方:`This function does not support data aggregation, multiple values will result in a MultiIndex in the columns.`\n",
    "\n",
    "几个示例：\n",
    "```\n",
    "Examples\n",
    "--------\n",
    ">>> df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',\n",
    "...                            'two'],\n",
    "...                    'bar': ['A', 'B', 'C', 'A', 'B', 'C'],\n",
    "...                    'baz': [1, 2, 3, 4, 5, 6],\n",
    "...                    'zoo': ['x', 'y', 'z', 'q', 'w', 't']})\n",
    ">>> df\n",
    "    foo   bar  baz  zoo\n",
    "0   one   A    1    x\n",
    "1   one   B    2    y\n",
    "2   one   C    3    z\n",
    "3   two   A    4    q\n",
    "4   two   B    5    w\n",
    "5   two   C    6    t\n",
    "\n",
    ">>> df.pivot(index='foo', columns='bar', values='baz')\n",
    "bar  A   B   C\n",
    "foo\n",
    "one  1   2   3\n",
    "two  4   5   6\n",
    "\n",
    ">>> df.pivot(index='foo', columns='bar')['baz']#教材中没有提到的可以省略value的操作，结果不变\n",
    "bar  A   B   C\n",
    "foo\n",
    "one  1   2   3\n",
    "two  4   5   6\n",
    "\n",
    ">>> df.pivot(index='foo', columns='bar', values=['baz', 'zoo'])\n",
    "      baz       zoo\n",
    "bar   A  B  C   A  B  C\n",
    "foo\n",
    "one   1  2  3   x  y  z\n",
    "two   4  5  6   q  w  t\n",
    "\n",
    "You could also assign a list of column names or a list of index names.\n",
    "\n",
    ">>> df = pd.DataFrame({\n",
    "...        \"lev1\": [1, 1, 1, 2, 2, 2],\n",
    "...        \"lev2\": [1, 1, 2, 1, 1, 2],\n",
    "...        \"lev3\": [1, 2, 1, 2, 1, 2],\n",
    "...        \"lev4\": [1, 2, 3, 4, 5, 6],\n",
    "...        \"values\": [0, 1, 2, 3, 4, 5]})\n",
    ">>> df\n",
    "    lev1 lev2 lev3 lev4 values\n",
    "0   1    1    1    1    0\n",
    "1   1    1    2    2    1\n",
    "2   1    2    1    3    2\n",
    "3   2    1    2    4    3\n",
    "4   2    1    1    5    4\n",
    "5   2    2    2    6    5\n",
    "\n",
    ">>> df.pivot(index=\"lev1\", columns=[\"lev2\", \"lev3\"],values=\"values\")\n",
    "lev2    1         2\n",
    "lev3    1    2    1    2\n",
    "lev1\n",
    "1     0.0  1.0  2.0  NaN\n",
    "2     4.0  3.0  NaN  5.0\n",
    "\n",
    ">>> df.pivot(index=[\"lev1\", \"lev2\"], columns=[\"lev3\"],values=\"values\")#改变index和column\n",
    "      lev3    1    2\n",
    "lev1  lev2\n",
    "   1     1  0.0  1.0\n",
    "         2  2.0  NaN\n",
    "   2     1  4.0  3.0\n",
    "         2  NaN  5.0\n",
    "#在pivot过程中，index变多，这个变形后的DF也变大了\n",
    "\n",
    "A ValueError is raised if there are any duplicates.\n",
    "\n",
    ">>> df = pd.DataFrame({\"foo\": ['one', 'one', 'two', 'two'],\n",
    "...                    \"bar\": ['A', 'A', 'B', 'C'],\n",
    "...                    \"baz\": [1, 2, 3, 4]})\n",
    ">>> df\n",
    "   foo bar  baz\n",
    "0  one   A    1\n",
    "1  one   A    2\n",
    "2  two   B    3\n",
    "3  two   C    4\n",
    "\n",
    "Notice that the first two rows are the same for our `index`\n",
    "and `columns` arguments.\n",
    "\n",
    ">>> df.pivot(index='foo', columns='bar', values='baz')\n",
    "Traceback (most recent call last):\n",
    "   ...\n",
    "ValueError: Index contains duplicate entries, cannot reshape\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "根据唯一性原则，新表的行索引等价于对`index`中的多列使用`drop_duplicates`，而列索引的长度为`values`中的元素个数乘以`columns`的唯一组合数量（与`index`类似） 。从下面的示意图中能够比较容易地理解相应的操作：\n",
    "\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/f0c1d6588e7243b6815bcfc77311a69233f5472b495d45859d8b70f1b45815b5)\n",
    "\n",
    "\n",
    "### 2. pivot_table\n",
    "\n",
    "`pivot`的使用依赖于唯一性条件，那如果不满足唯一性条件，那么必须通过聚合操作使得相同行列组合对应的多个值变为一个值。例如，张三和李四都参加了两次语文考试和数学考试，按照学院规定，最后的成绩是两次考试分数的平均值，此时就无法通过`pivot`函数来完成。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Subject</th>\n",
       "      <th>Grade</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Math</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Math</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Si Li</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Si Li</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Si Li</td>\n",
       "      <td>Math</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Si Li</td>\n",
       "      <td>Math</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Name  Subject  Grade\n",
       "0  San Zhang  Chinese     80\n",
       "1  San Zhang  Chinese     90\n",
       "2  San Zhang     Math    100\n",
       "3  San Zhang     Math     90\n",
       "4      Si Li  Chinese     70\n",
       "5      Si Li  Chinese     80\n",
       "6      Si Li     Math     85\n",
       "7      Si Li     Math     95"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'Name':['San Zhang', 'San Zhang', \n",
    "                              'San Zhang', 'San Zhang',\n",
    "                              'Si Li', 'Si Li', 'Si Li', 'Si Li'],\n",
    "                   'Subject':['Chinese', 'Chinese', 'Math', 'Math',\n",
    "                                 'Chinese', 'Chinese', 'Math', 'Math'],\n",
    "                   'Grade':[80, 90, 100, 90, 70, 80, 85, 95]})\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "`pandas`中提供了`pivot_table`来实现，其中的`aggfunc`参数就是使用的聚合函数。上述场景可以如下写出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Subject</th>\n",
       "      <th>Chinese</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>San Zhang</th>\n",
       "      <td>85</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Si Li</th>\n",
       "      <td>75</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Subject    Chinese  Math\n",
       "Name                    \n",
       "San Zhang       85    95\n",
       "Si Li           75    90"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(index = 'Name',\n",
    "               columns = 'Subject',\n",
    "               values = 'Grade',\n",
    "               aggfunc = 'mean')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "这里传入`aggfunc`包含了上一章中介绍的所有合法聚合字符串，此外还可以传入以序列为输入标量为输出的聚合函数来实现自定义操作，上述功能可以等价写出："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Subject</th>\n",
       "      <th>Chinese</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>San Zhang</th>\n",
       "      <td>85</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Si Li</th>\n",
       "      <td>75</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Subject    Chinese  Math\n",
       "Name                    \n",
       "San Zhang       85    95\n",
       "Si Li           75    90"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(index = 'Name',\n",
    "               columns = 'Subject',\n",
    "               values = 'Grade',\n",
    "               aggfunc = lambda x:x.mean())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "复习一下上一节的可用的聚合函数：\n",
    "参考自https://pandas.pydata.org/docs/reference/groupby.html\n",
    "|函数|用途|\n",
    "|--|--|\n",
    "max|Compute max of group values\n",
    "min|Compute min of group values.\n",
    "mean|Compute mean of groups, excluding missing values.\n",
    "median|Compute median of groups, excluding missing values.\n",
    "count|Compute count of group, excluding missing values.\n",
    "all|Return True if all values in the group are truthful, else False\n",
    "any|Return True if any value in the group is truthful, else False.\n",
    "idmax|Return index of first occurrence of maximum over requested axis.\n",
    "idmin|Return index of first occurrence of minimum over requested axis.\n",
    "mad|Return the mean absolute deviation of the values for the requested axis.\n",
    "nunique|Return DataFrame with counts of unique elements in each position.\n",
    "skew|Return unbiased skew over requested axis. skew 偏差\n",
    "quantile|Return group values at the given quantile, a la numpy.percentile. 分位数\n",
    "sum|Compute sum of group values.\n",
    "std|Compute standard deviation of groups, excluding missing values.\n",
    "var|Compute variance of groups, excluding missing values.\n",
    "sem|Compute standard error of the mean of groups, excluding missing values.\n",
    "size|Compute group sizes.\n",
    "prob|Compute prod of group values.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "此外，`pivot_table`具有边际汇总的功能，可以通过设置`margins=True`来实现，其中边际的聚合方式与`aggfunc`中给出的聚合方法一致。下面就分别统计了语文均分和数学均分、张三均分和李四均分，以及总体所有分数的均分："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df.pivot_table(index,columns,values,aggfunc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Subject</th>\n",
       "      <th>Chinese</th>\n",
       "      <th>Math</th>\n",
       "      <th>All</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>San Zhang</th>\n",
       "      <td>85</td>\n",
       "      <td>95.0</td>\n",
       "      <td>90.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Si Li</th>\n",
       "      <td>75</td>\n",
       "      <td>90.0</td>\n",
       "      <td>82.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>All</th>\n",
       "      <td>80</td>\n",
       "      <td>92.5</td>\n",
       "      <td>86.25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Subject    Chinese  Math    All\n",
       "Name                           \n",
       "San Zhang       85  95.0  90.00\n",
       "Si Li           75  90.0  82.50\n",
       "All             80  92.5  86.25"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(index = 'Name',\n",
    "               columns = 'Subject',\n",
    "               values = 'Grade',\n",
    "               aggfunc='mean',\n",
    "               margins=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "看一看官方文档`df.pivot_table?\n",
    "`\n",
    "首先是用法，所有可以用的功能：\n",
    "```\n",
    "df.pivot_table(\n",
    "    values=None,\n",
    "    index=None,\n",
    "    columns=None,\n",
    "    aggfunc='mean',\n",
    "    fill_value=None,\n",
    "    margins=False,\n",
    "    dropna=True,\n",
    "    margins_name='All',\n",
    "    observed=False,\n",
    ") -> 'DataFrame'\n",
    "```\n",
    "具体功能：\n",
    "\n",
    "```\n",
    "\n",
    "Parameters\n",
    "----------\n",
    "values : column to aggregate, optional\n",
    "index : column, Grouper, array, or list of the previous\n",
    "    If an array is passed, it must be the same length as the data. The\n",
    "    list can contain any of the other types (except list).\n",
    "    Keys to group by on the pivot table index.  If an array is passed,\n",
    "    it is being used as the same manner as column values.\n",
    "    list夹list不行\n",
    "columns : column, Grouper, array, or list of the previous\n",
    "    If an array is passed, it must be the same length as the data. The\n",
    "    list can contain any of the other types (except list).\n",
    "    Keys to group by on the pivot table column.  If an array is passed,\n",
    "    it is being used as the same manner as column values.\n",
    "    list夹list不行\n",
    "aggfunc : function, list of functions, dict, default numpy.mean\n",
    "    If list of functions passed, the resulting pivot table will have\n",
    "    hierarchical columns whose top level are the function names\n",
    "    (inferred from the function objects themselves)\n",
    "    If dict is passed, the key is column to aggregate and value\n",
    "    is function or list of functions.\n",
    "    默认取平均值，可以使用字典传入列名和函数功能\n",
    "fill_value : scalar, default None\n",
    "    Value to replace missing values with (in the resulting pivot table,\n",
    "    after aggregation).\n",
    "margins : bool, default False\n",
    "    Add all row / columns (e.g. for subtotal / grand totals).\n",
    "dropna : bool, default True\n",
    "    Do not include columns whose entries are all NaN\n",
    "    是否去掉空值\n",
    "margins_name : str, default 'All'\n",
    "    Name of the row / column that will contain the totals\n",
    "    when margins is True.\n",
    "observed : bool, default False\n",
    "    This only applies if any of the groupers are Categoricals.\n",
    "    If True: only show observed values for categorical groupers.\n",
    "    If False: show all values for categorical groupers.\n",
    "\n",
    "```\n",
    "一些例子：\n",
    "```\n",
    "Examples\n",
    "--------\n",
    ">>> df = pd.DataFrame({\"A\": [\"foo\", \"foo\", \"foo\", \"foo\", \"foo\",\n",
    "...                          \"bar\", \"bar\", \"bar\", \"bar\"],\n",
    "...                    \"B\": [\"one\", \"one\", \"one\", \"two\", \"two\",\n",
    "...                          \"one\", \"one\", \"two\", \"two\"],\n",
    "...                    \"C\": [\"small\", \"large\", \"large\", \"small\",\n",
    "...                          \"small\", \"large\", \"small\", \"small\",\n",
    "...                          \"large\"],\n",
    "...                    \"D\": [1, 2, 2, 3, 3, 4, 5, 6, 7],\n",
    "...                    \"E\": [2, 4, 5, 5, 6, 6, 8, 9, 9]})\n",
    ">>> df\n",
    "     A    B      C  D  E\n",
    "0  foo  one  small  1  2\n",
    "1  foo  one  large  2  4\n",
    "2  foo  one  large  2  5\n",
    "3  foo  two  small  3  5\n",
    "4  foo  two  small  3  6\n",
    "5  bar  one  large  4  6\n",
    "6  bar  one  small  5  8\n",
    "7  bar  two  small  6  9\n",
    "8  bar  two  large  7  9\n",
    "\n",
    "This first example aggregates values by taking the sum.\n",
    "\n",
    ">>> table = pd.pivot_table(df, values='D', index=['A', 'B'],\n",
    "...                     columns=['C'], aggfunc=np.sum)\n",
    ">>> table\n",
    "C        large  small\n",
    "A   B\n",
    "bar one    4.0    5.0\n",
    "    two    7.0    6.0\n",
    "foo one    4.0    1.0\n",
    "    two    NaN    6.0\n",
    "\n",
    "We can also fill missing values using the `fill_value` parameter.\n",
    "\n",
    ">>> table = pd.pivot_table(df, values='D', index=['A', 'B'],\n",
    "...                     columns=['C'], aggfunc=np.sum, fill_value=0)\n",
    ">>> table\n",
    "C        large  small\n",
    "A   B\n",
    "bar one      4      5\n",
    "    two      7      6\n",
    "foo one      4      1\n",
    "    two      0      6\n",
    "\n",
    "The next example aggregates by taking the mean across multiple columns.\n",
    "这个例子只对部分列做了计算\n",
    ">>> table = pd.pivot_table(df, values=['D', 'E'], index=['A', 'C'],\n",
    "...                     aggfunc={'D': np.mean,\n",
    "...                              'E': np.mean})\n",
    ">>> table\n",
    "                D         E\n",
    "A   C\n",
    "bar large  5.500000  7.500000\n",
    "    small  5.500000  8.500000\n",
    "foo large  2.000000  4.500000\n",
    "    small  2.333333  4.333333\n",
    "\n",
    "We can also calculate multiple types of aggregations for any given\n",
    "value column.\n",
    "\n",
    "这个例子中传入的函数有list，E三种都算了\n",
    ">>> table = pd.pivot_table(df, values=['D', 'E'], index=['A', 'C'],\n",
    "...                     aggfunc={'D': np.mean,\n",
    "...                              'E': [min, max, np.mean]})\n",
    ">>> table\n",
    "                D    E\n",
    "            mean  max      mean  min\n",
    "A   C\n",
    "bar large  5.500000  9.0  7.500000  6.0\n",
    "    small  5.500000  9.0  8.500000  8.0\n",
    "foo large  2.000000  5.0  4.500000  4.0\n",
    "    small  2.333333  6.0  4.333333  2.0\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "#### 【练一练】\n",
    "在上面的边际汇总例子中，行或列的汇总为新表中行元素或者列元素的平均值，而总体的汇总为新表中四个元素的平均值。这种关系一定成立吗？若不成立，请给出一个例子来说明。\n",
    "\n",
    "先来测试一下把的aggfun换成别的函数的情况。\n",
    "\n",
    "换成了max以后，边际汇总也变成了max\n",
    "\n",
    "所以总体的汇总不一定是新表中四个元素的平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Subject</th>\n",
       "      <th>Chinese</th>\n",
       "      <th>Math</th>\n",
       "      <th>All</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>San Zhang</th>\n",
       "      <td>90</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Si Li</th>\n",
       "      <td>80</td>\n",
       "      <td>95</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>All</th>\n",
       "      <td>90</td>\n",
       "      <td>100</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Subject    Chinese  Math  All\n",
       "Name                         \n",
       "San Zhang       90   100  100\n",
       "Si Li           80    95   95\n",
       "All             90   100  100"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(index = 'Name',\r\n",
    "               columns = 'Subject',\r\n",
    "               values = 'Grade',\r\n",
    "               aggfunc='max',\r\n",
    "               margins=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "\n",
    "#### 【END】\n",
    "### 3. melt\n",
    "\n",
    "长宽表只是数据呈现方式的差异，但其包含的信息量是等价的，前面提到了利用`pivot`把长表转为宽表，那么就可以通过相应的逆操作把宽表转为长表，`melt`函数就起到了这样的作用。在下面的例子中，`Subject`以列索引的形式存储，现在想要将其压缩到一个列中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Name</th>\n",
       "      <th>Chinese</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>80</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>90</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Class       Name  Chinese  Math\n",
       "0      1  San Zhang       80    80\n",
       "1      2      Si Li       90    75"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'Class':[1,2],\n",
    "                   'Name':['San Zhang', 'Si Li'],\n",
    "                   'Chinese':[80, 90],\n",
    "                   'Math':[80, 75]})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Name</th>\n",
       "      <th>Subject</th>\n",
       "      <th>Grade</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Math</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Math</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Class       Name  Subject  Grade\n",
       "0      1  San Zhang  Chinese     80\n",
       "1      2      Si Li  Chinese     90\n",
       "2      1  San Zhang     Math     80\n",
       "3      2      Si Li     Math     75"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_melted = df.melt(id_vars = ['Class', 'Name'],\n",
    "                    value_vars = ['Chinese', 'Math'],\n",
    "                    var_name = 'Subject',\n",
    "                    value_name = 'Grade')\n",
    "df_melted"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "`melt`的主要参数和压缩的过程如下图所示：\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/82de1269f9bb44e28241331eb00d1180292ddee4bb6e4d88b7a6718a779881d0)\n",
    "\n",
    "\n",
    "\n",
    "前面提到了`melt`和`pivot`是一组互逆过程，那么就一定可以通过`pivot`操作把`df_melted`转回`df`的形式："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Subject</th>\n",
       "      <th>Chinese</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Class</th>\n",
       "      <th>Name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <th>San Zhang</th>\n",
       "      <td>80</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <th>Si Li</th>\n",
       "      <td>90</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Subject          Chinese  Math\n",
       "Class Name                    \n",
       "1     San Zhang       80    80\n",
       "2     Si Li           90    75"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_unmelted = df_melted.pivot(index = ['Class', 'Name'],\n",
    "                              columns='Subject',\n",
    "                              values='Grade')\n",
    "df_unmelted # 下面需要恢复索引，并且重命名列索引名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_unmelted = df_unmelted.reset_index().rename_axis(columns={'Subject':''})\n",
    "df_unmelted.equals(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "看一看官方文档`df.melt?`\n",
    "首先是输入输出：\n",
    "```\n",
    "df.melt(\n",
    "    id_vars=None,\n",
    "    value_vars=None,\n",
    "    var_name=None,\n",
    "    value_name='value',\n",
    "    col_level=None,\n",
    "    ignore_index=True,\n",
    ") -> 'DataFrame'\n",
    "```\n",
    "\n",
    "对于功能的描述很棒：  \n",
    "**a format where one or more columns are identifier variables (`id_vars`), while all other\n",
    "columns, considered measured variables (`value_vars`), are \"unpivoted\" to\n",
    "the row axis, leaving just two non-identifier columns, 'variable' and\n",
    "'value'.**\n",
    "结合作者大哥的图解就更好理解了\n",
    "```\n",
    "Docstring:\n",
    "Unpivot a DataFrame from wide to long format, optionally leaving identifiers set.\n",
    "\n",
    "This function is useful to massage a DataFrame into a format where one\n",
    "or more columns are identifier variables (`id_vars`), while all other\n",
    "columns, considered measured variables (`value_vars`), are \"unpivoted\" to\n",
    "the row axis, leaving just two non-identifier columns, 'variable' and\n",
    "'value'.\n",
    "```\n",
    "具体使用方法：\n",
    "```\n",
    "Parameters\n",
    "----------\n",
    "id_vars : tuple, list, or ndarray, optional\n",
    "    Column(s) to use as identifier variables.\n",
    "value_vars : tuple, list, or ndarray, optional\n",
    "    Column(s) to unpivot. If not specified, uses all columns that\n",
    "    are not set as `id_vars`.\n",
    "var_name : scalar\n",
    "    Name to use for the 'variable' column. If None it uses\n",
    "    ``frame.columns.name`` or 'variable'.\n",
    "value_name : scalar, default 'value'\n",
    "    Name to use for the 'value' column.\n",
    "col_level : int or str, optional\n",
    "    If columns are a MultiIndex then use this level to melt.\n",
    "    多级索引用\n",
    "ignore_index : bool, default True\n",
    "    If True, original index is ignored. If False, the original index is retained.\n",
    "    Index labels will be repeated as necessary.\n",
    "    是否忽略原始的茵蒂克丝\n",
    "\n",
    "```\n",
    "\n",
    "一些例子：\n",
    "```\n",
    "\n",
    "Examples\n",
    "--------\n",
    ">>> df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},\n",
    "...                    'B': {0: 1, 1: 3, 2: 5},\n",
    "...                    'C': {0: 2, 1: 4, 2: 6}})\n",
    ">>> df\n",
    "   A  B  C\n",
    "0  a  1  2\n",
    "1  b  3  4\n",
    "2  c  5  6\n",
    "\n",
    ">>> df.melt(id_vars=['A'], value_vars=['B'])\n",
    "   A variable  value\n",
    "0  a        B      1\n",
    "1  b        B      3\n",
    "2  c        B      5\n",
    "\n",
    ">>> df.melt(id_vars=['A'], value_vars=['B', 'C'])\n",
    "   A variable  value\n",
    "0  a        B      1\n",
    "1  b        B      3\n",
    "2  c        B      5\n",
    "3  a        C      2\n",
    "4  b        C      4\n",
    "5  c        C      6\n",
    "\n",
    "The names of 'variable' and 'value' columns can be customized:\n",
    "可以对行列重命名\n",
    ">>> df.melt(id_vars=['A'], value_vars=['B'],\n",
    "...         var_name='myVarname', value_name='myValname')\n",
    "   A myVarname  myValname\n",
    "0  a         B          1\n",
    "1  b         B          3\n",
    "2  c         B          5\n",
    "\n",
    "Original index values can be kept around:\n",
    "可以忽略原始的茵蒂克丝\n",
    ">>> df.melt(id_vars=['A'], value_vars=['B', 'C'], ignore_index=False)\n",
    "   A variable  value\n",
    "0  a        B      1\n",
    "1  b        B      3\n",
    "2  c        B      5\n",
    "0  a        C      2\n",
    "1  b        C      4\n",
    "2  c        C      6\n",
    "\n",
    "If you have multi-index columns:\n",
    "\n",
    ">>> df.columns = [list('ABC'), list('DEF')]\n",
    ">>> df\n",
    "   A  B  C\n",
    "   D  E  F\n",
    "0  a  1  2\n",
    "1  b  3  4\n",
    "2  c  5  6\n",
    "\n",
    ">>> df.melt(col_level=0, id_vars=['A'], value_vars=['B'])\n",
    "   A variable  value\n",
    "0  a        B      1\n",
    "1  b        B      3\n",
    "2  c        B      5\n",
    "如果是多层的茵蒂克丝，会形成tuple\n",
    ">>> df.melt(id_vars=[('A', 'D')], value_vars=[('B', 'E')])\n",
    "  (A, D) variable_0 variable_1  value\n",
    "0      a          B          E      1\n",
    "1      b          B          E      3\n",
    "2      c          B          E      5\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### 4. wide_to_long\n",
    "\n",
    "`melt`方法中，在列索引中被压缩的一组值对应的列元素只能代表同一层次的含义，即`values_name`。现在如果列中包含了交叉类别，比如期中期末的类别和语文数学的类别，那么想要把`values_name`对应的`Grade`扩充为两列分别对应语文分数和数学分数，只把期中期末的信息压缩，这种需求下就要使用`wide_to_long`函数来完成。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Name</th>\n",
       "      <th>Chinese_Mid</th>\n",
       "      <th>Math_Mid</th>\n",
       "      <th>Chinese_Final</th>\n",
       "      <th>Math_Final</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Class       Name  Chinese_Mid  Math_Mid  Chinese_Final  Math_Final\n",
       "0      1  San Zhang           80        90             80          90\n",
       "1      2      Si Li           75        85             75          85"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'Class':[1,2],'Name':['San Zhang', 'Si Li'],\n",
    "                   'Chinese_Mid':[80, 75], 'Math_Mid':[90, 85],\n",
    "                   'Chinese_Final':[80, 75], 'Math_Final':[90, 85]})\n",
    "df\n",
    "#这里定义的Chinese和Math对应的考试不止一场"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Chinese</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Class</th>\n",
       "      <th>Name</th>\n",
       "      <th>Examination</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">San Zhang</th>\n",
       "      <th>Mid</th>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Final</th>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">Si Li</th>\n",
       "      <th>Mid</th>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Final</th>\n",
       "      <td>75</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             Chinese  Math\n",
       "Class Name      Examination               \n",
       "1     San Zhang Mid               80    90\n",
       "                Final             80    90\n",
       "2     Si Li     Mid               75    85\n",
       "                Final             75    85"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.wide_to_long(df,\n",
    "                stubnames=['Chinese', 'Math'],\n",
    "                i = ['Class', 'Name'],\n",
    "                j='Examination',\n",
    "                sep='_',\n",
    "                suffix='.+')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "具体的变换过程由下图进行展示，属相同概念的元素使用了一致的颜色标出：\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/b1b9b49dc22140dcb3e1b1798999946a20a3fdb2bc7348ff8b5f76e36b076bae)\n",
    "\n",
    "\n",
    "下面给出一个比较复杂的案例，把之前在`pivot`一节中多列操作的结果（产生了多级索引），利用`wide_to_long`函数，将其转为原来的形态。其中，使用了第八章的`str.split`函数，目前暂时只需将其理解为对序列按照某个分隔符进行拆分即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Name</th>\n",
       "      <th>Examination</th>\n",
       "      <th>Subject</th>\n",
       "      <th>Grade</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Mid</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>80</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Final</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>75</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Mid</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>85</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Final</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>65</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Mid</td>\n",
       "      <td>Math</td>\n",
       "      <td>90</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Final</td>\n",
       "      <td>Math</td>\n",
       "      <td>85</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Mid</td>\n",
       "      <td>Math</td>\n",
       "      <td>92</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Final</td>\n",
       "      <td>Math</td>\n",
       "      <td>88</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Class       Name Examination  Subject  Grade  rank\n",
       "0      1  San Zhang         Mid  Chinese     80    10\n",
       "1      1  San Zhang       Final  Chinese     75    15\n",
       "2      2      Si Li         Mid  Chinese     85    21\n",
       "3      2      Si Li       Final  Chinese     65    15\n",
       "4      1  San Zhang         Mid     Math     90    20\n",
       "5      1  San Zhang       Final     Math     85     7\n",
       "6      2      Si Li         Mid     Math     92     6\n",
       "7      2      Si Li       Final     Math     88     2"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res = pivot_multi.copy()\n",
    "res.columns = res.columns.map(lambda x:'_'.join(x))\n",
    "res = res.reset_index()\n",
    "res = pd.wide_to_long(res, stubnames=['Grade', 'rank'],\n",
    "                           i = ['Class', 'Name'],\n",
    "                           j = 'Subject_Examination',\n",
    "                           sep = '_',\n",
    "                           suffix = '.+')\n",
    "res = res.reset_index()\n",
    "res[['Subject', 'Examination']] = res['Subject_Examination'].str.split('_', expand=True)\n",
    "res = res[['Class', 'Name', 'Examination', 'Subject', 'Grade', 'rank']].sort_values('Subject')\n",
    "res = res.reset_index(drop=True)\n",
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "看一看官方文档`pd.wide_to_long?`\n",
    "\n",
    "首先是输入输出\n",
    "```\n",
    "pd.wide_to_long(\n",
    "    df: 'DataFrame',\n",
    "    stubnames,\n",
    "    i,\n",
    "    j,\n",
    "    sep: str = '',\n",
    "    suffix: str = '\\\\d+',\n",
    ") -> 'DataFrame'\n",
    "Returns\n",
    "-------\n",
    "DataFrame\n",
    "    A DataFrame that contains each stub name as a variable, with new index\n",
    "    (i, j).\n",
    "```\n",
    "功能的介绍：\n",
    "这里面 Less flexible but more user-friendly就很好玩，还提到每个要assume的变量需要被i能唯一区分开\n",
    "```\n",
    "Docstring:\n",
    "Wide panel to long format. Less flexible but more user-friendly than melt.\n",
    "With stubnames ['A', 'B'], this function expects to find one or more\n",
    "group of columns with format\n",
    "A-suffix1, A-suffix2,..., B-suffix1, B-suffix2,...\n",
    "You specify what you want to call this suffix in the resulting long format\n",
    "with `j` (for example `j='year'`)\n",
    "Each row of these wide variables are assumed to be uniquely identified by\n",
    "`i` (can be a single column name or a list of column names)\n",
    "All remaining variables in the data frame are left intact.\n",
    "```\n",
    "\n",
    "函数的可用功能：其中正则后缀suffix值得看一下\n",
    "```\n",
    "Parameters\n",
    "----------\n",
    "df : DataFrame\n",
    "    The wide-format DataFrame.\n",
    "stubnames : str or list-like\n",
    "    The stub name(s). The wide format variables are assumed to\n",
    "    start with the stub names.\n",
    "i : str or list-like\n",
    "    Column(s) to use as id variable(s).\n",
    "j : str\n",
    "    The name of the sub-observation variable. What you wish to name your\n",
    "    suffix in the long format.\n",
    "sep : str, default \"\"\n",
    "    A character indicating the separation of the variable names\n",
    "    in the wide format, to be stripped from the names in the long format.\n",
    "    For example, if your column names are A-suffix1, A-suffix2, you\n",
    "    can strip the hyphen by specifying `sep='-'`.\n",
    "suffix : str, default '\\\\d+'\n",
    "    A regular expression capturing the wanted suffixes. '\\\\d+' captures\n",
    "    numeric suffixes. Suffixes with no numbers could be specified with the\n",
    "    negated character class '\\\\D+'. You can also further disambiguate\n",
    "    suffixes, for example, if your wide variables are of the form\n",
    "    A-one, B-two,.., and you have an unrelated column A-rating, you can\n",
    "    ignore the last one by specifying `suffix='(!?one|two)'`.\n",
    "\t\t用`suffix='(!?one|two)'`把不相干A-rating区分\n",
    "\n",
    "```\n",
    "\n",
    "一些例子：\n",
    "```\n",
    "Examples\n",
    "--------\n",
    ">>> np.random.seed(123)\n",
    ">>> df = pd.DataFrame({\"A1970\" : {0 : \"a\", 1 : \"b\", 2 : \"c\"},\n",
    "...                    \"A1980\" : {0 : \"d\", 1 : \"e\", 2 : \"f\"},\n",
    "...                    \"B1970\" : {0 : 2.5, 1 : 1.2, 2 : .7},\n",
    "...                    \"B1980\" : {0 : 3.2, 1 : 1.3, 2 : .1},\n",
    "...                    \"X\"     : dict(zip(range(3), np.random.randn(3)))\n",
    "...                   })\n",
    ">>> df[\"id\"] = df.index\n",
    ">>> df\n",
    "  A1970 A1980  B1970  B1980         X  id\n",
    "0     a     d    2.5    3.2 -1.085631   0\n",
    "1     b     e    1.2    1.3  0.997345   1\n",
    "2     c     f    0.7    0.1  0.282978   2\n",
    "\n",
    "使用,保持不变的是id是年份，压成新的列标签的是year\n",
    ">>> pd.wide_to_long(df, [\"A\", \"B\"], i=\"id\", j=\"year\")\n",
    "... # doctest: +NORMALIZE_WHITESPACE\n",
    "                X  A    B\n",
    "id year\n",
    "0  1970 -1.085631  a  2.5\n",
    "1  1970  0.997345  b  1.2\n",
    "2  1970  0.282978  c  0.7\n",
    "0  1980 -1.085631  d  3.2\n",
    "1  1980  0.997345  e  1.3\n",
    "2  1980  0.282978  f  0.1\n",
    "\n",
    "With multiple id columns\n",
    "多极标签的转化\n",
    ">>> df = pd.DataFrame({\n",
    "...     'famid': [1, 1, 1, 2, 2, 2, 3, 3, 3],\n",
    "...     'birth': [1, 2, 3, 1, 2, 3, 1, 2, 3],\n",
    "...     'ht1': [2.8, 2.9, 2.2, 2, 1.8, 1.9, 2.2, 2.3, 2.1],\n",
    "...     'ht2': [3.4, 3.8, 2.9, 3.2, 2.8, 2.4, 3.3, 3.4, 2.9]\n",
    "... })\n",
    ">>> df\n",
    "   famid  birth  ht1  ht2\n",
    "0      1      1  2.8  3.4\n",
    "1      1      2  2.9  3.8\n",
    "2      1      3  2.2  2.9\n",
    "3      2      1  2.0  3.2\n",
    "4      2      2  1.8  2.8\n",
    "5      2      3  1.9  2.4\n",
    "6      3      1  2.2  3.3\n",
    "7      3      2  2.3  3.4\n",
    "8      3      3  2.1  2.9\n",
    ">>> l = pd.wide_to_long(df, stubnames='ht', i=['famid', 'birth'], j='age')\n",
    "\n",
    "ht是转换的变量值，famid和birth保持不变，age压成新的列标签\n",
    ">>> l\n",
    "... # doctest: +NORMALIZE_WHITESPACE\n",
    "                  ht\n",
    "famid birth age\n",
    "1     1     1    2.8\n",
    "            2    3.4\n",
    "      2     1    2.9\n",
    "            2    3.8\n",
    "      3     1    2.2\n",
    "            2    2.9\n",
    "2     1     1    2.0\n",
    "            2    3.2\n",
    "      2     1    1.8\n",
    "            2    2.8\n",
    "      3     1    1.9\n",
    "            2    2.4\n",
    "3     1     1    2.2\n",
    "            2    3.3\n",
    "      2     1    2.3\n",
    "            2    3.4\n",
    "      3     1    2.1\n",
    "            2    2.9\n",
    "\n",
    "Going from long back to wide just takes some creative use of `unstack`\n",
    "\n",
    ">>> w = l.unstack()\n",
    "行列互转\n",
    ">>> w.columns = w.columns.map('{0[0]}{0[1]}'.format)\n",
    ">>> w.reset_index()\n",
    "   famid  birth  ht1  ht2\n",
    "0      1      1  2.8  3.4\n",
    "1      1      2  2.9  3.8\n",
    "2      1      3  2.2  2.9\n",
    "3      2      1  2.0  3.2\n",
    "4      2      2  1.8  2.8\n",
    "5      2      3  1.9  2.4\n",
    "6      3      1  2.2  3.3\n",
    "7      3      2  2.3  3.4\n",
    "8      3      3  2.1  2.9\n",
    "\n",
    "Less wieldy column names are also handled\n",
    "\n",
    ">>> np.random.seed(0)\n",
    ">>> df = pd.DataFrame({'A(weekly)-2010': np.random.rand(3),\n",
    "...                    'A(weekly)-2011': np.random.rand(3),\n",
    "...                    'B(weekly)-2010': np.random.rand(3),\n",
    "...                    'B(weekly)-2011': np.random.rand(3),\n",
    "...                    'X' : np.random.randint(3, size=3)})\n",
    ">>> df['id'] = df.index\n",
    ">>> df # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS\n",
    "   A(weekly)-2010  A(weekly)-2011  B(weekly)-2010  B(weekly)-2011  X  id\n",
    "0        0.548814        0.544883        0.437587        0.383442  0   0\n",
    "1        0.715189        0.423655        0.891773        0.791725  1   1\n",
    "2        0.602763        0.645894        0.963663        0.528895  1   2\n",
    "这里分隔符是-，保持不变的是[A()，B()],year是新的标签\n",
    ">>> pd.wide_to_long(df, ['A(weekly)', 'B(weekly)'], i='id',\n",
    "...                 j='year', sep='-')\n",
    "... # doctest: +NORMALIZE_WHITESPACE\n",
    "         X  A(weekly)  B(weekly)\n",
    "id year\n",
    "0  2010  0   0.548814   0.437587\n",
    "1  2010  1   0.715189   0.891773\n",
    "2  2010  1   0.602763   0.963663\n",
    "0  2011  0   0.544883   0.383442\n",
    "1  2011  1   0.423655   0.791725\n",
    "2  2011  1   0.645894   0.528895\n",
    "\n",
    "If we have many columns, we could also use a regex to find our\n",
    "stubnames and pass that list on to wide_to_long\n",
    "\n",
    ">>> stubnames = sorted(\n",
    "...     set([match[0] for match in df.columns.str.findall(\n",
    "...         r'[A-B]\\(.*\\)').values if match != []])\n",
    "... )\n",
    ">>> list(stubnames)\n",
    "['A(weekly)', 'B(weekly)']\n",
    "\n",
    "All of the above examples have integers as suffixes. It is possible to\n",
    "have non-integers as suffixes.\n",
    "使用不是整数的做正则后缀\n",
    ">>> df = pd.DataFrame({\n",
    "...     'famid': [1, 1, 1, 2, 2, 2, 3, 3, 3],\n",
    "...     'birth': [1, 2, 3, 1, 2, 3, 1, 2, 3],\n",
    "...     'ht_one': [2.8, 2.9, 2.2, 2, 1.8, 1.9, 2.2, 2.3, 2.1],\n",
    "...     'ht_two': [3.4, 3.8, 2.9, 3.2, 2.8, 2.4, 3.3, 3.4, 2.9]\n",
    "... })\n",
    ">>> df\n",
    "   famid  birth  ht_one  ht_two\n",
    "0      1      1     2.8     3.4\n",
    "1      1      2     2.9     3.8\n",
    "2      1      3     2.2     2.9\n",
    "3      2      1     2.0     3.2\n",
    "4      2      2     1.8     2.8\n",
    "5      2      3     1.9     2.4\n",
    "6      3      1     2.2     3.3\n",
    "7      3      2     2.3     3.4\n",
    "8      3      3     2.1     2.9\n",
    "ht是转换的变量，famid和birth不变，age成为新的列标签\n",
    ">>> l = pd.wide_to_long(df, stubnames='ht', i=['famid', 'birth'], j='age',\n",
    "...                     sep='_', suffix='\\w+')\n",
    ">>> l\n",
    "... # doctest: +NORMALIZE_WHITESPACE\n",
    "                  ht\n",
    "famid birth age\n",
    "1     1     one  2.8\n",
    "            two  3.4\n",
    "      2     one  2.9\n",
    "            two  3.8\n",
    "      3     one  2.2\n",
    "            two  2.9\n",
    "2     1     one  2.0\n",
    "            two  3.2\n",
    "      2     one  1.8\n",
    "            two  2.8\n",
    "      3     one  1.9\n",
    "            two  2.4\n",
    "3     1     one  2.2\n",
    "            two  3.3\n",
    "      2     one  2.3\n",
    "            two  3.4\n",
    "      3     one  2.1\n",
    "            two  2.9\n",
    " ```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 二、索引的变形\n",
    "\n",
    "### 1. stack与unstack\n",
    "\n",
    "在第二章中提到了利用`swaplevel`或者`reorder_levels`进行索引内部的层交换，下面就要讨论$\\color{red}{行列索引之间}$的交换，由于这种交换带来了`DataFrame`维度上的变化，因此属于变形操作。在第一节中提到的4种变形函数与其不同之处在于，它们都属于某一列或几列$\\color{red}{元素}$和$\\color{red}{列索引}$之间的转换，而不是索引之间的转换。\n",
    "\n",
    "`unstack`函数的作用是把行索引转为列索引，例如下面这个简单的例子：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>col_1</th>\n",
       "      <th>col_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">A</th>\n",
       "      <th>cat</th>\n",
       "      <th>big</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <th>small</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">B</th>\n",
       "      <th>cat</th>\n",
       "      <th>big</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <th>small</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             col_1  col_2\n",
       "A cat big      1.0    1.0\n",
       "  dog small    1.0    1.0\n",
       "B cat big      1.0    1.0\n",
       "  dog small    1.0    1.0"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.ones((4,2)),\n",
    "                  index = pd.Index([('A', 'cat', 'big'),\n",
    "                                    ('A', 'dog', 'small'),\n",
    "                                    ('B', 'cat', 'big'),\n",
    "                                    ('B', 'dog', 'small')]),\n",
    "                  columns=['col_1', 'col_2'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">col_1</th>\n",
       "      <th colspan=\"2\" halign=\"left\">col_2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>big</th>\n",
       "      <th>small</th>\n",
       "      <th>big</th>\n",
       "      <th>small</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">A</th>\n",
       "      <th>cat</th>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">B</th>\n",
       "      <th>cat</th>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      col_1       col_2      \n",
       "        big small   big small\n",
       "A cat   1.0   NaN   1.0   NaN\n",
       "  dog   NaN   1.0   NaN   1.0\n",
       "B cat   1.0   NaN   1.0   NaN\n",
       "  dog   NaN   1.0   NaN   1.0"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.unstack()\r\n",
    "#这里把大小的行索引给换到了列上"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "`unstack`的主要参数是移动的层号，默认转化最内层，移动到列索引的最内层，同时支持同时转化多个层："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      col_1       col_2      \n",
      "        big small   big small\n",
      "A cat   1.0   NaN   1.0   NaN\n",
      "  dog   NaN   1.0   NaN   1.0\n",
      "B cat   1.0   NaN   1.0   NaN\n",
      "  dog   NaN   1.0   NaN   1.0\n",
      "________________________________________\n",
      "             col_1  col_2\n",
      "A cat big      1.0    1.0\n",
      "  dog small    1.0    1.0\n",
      "B cat big      1.0    1.0\n",
      "  dog small    1.0    1.0\n"
     ]
    }
   ],
   "source": [
    "print(df.unstack(2))\r\n",
    "#这里是转化了第二层，大小，所以与上面的结果一样\r\n",
    "print(\"________________________________________\")\r\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"4\" halign=\"left\">col_1</th>\n",
       "      <th colspan=\"4\" halign=\"left\">col_2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">A</th>\n",
       "      <th colspan=\"2\" halign=\"left\">B</th>\n",
       "      <th colspan=\"2\" halign=\"left\">A</th>\n",
       "      <th colspan=\"2\" halign=\"left\">B</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>big</th>\n",
       "      <th>small</th>\n",
       "      <th>big</th>\n",
       "      <th>small</th>\n",
       "      <th>big</th>\n",
       "      <th>small</th>\n",
       "      <th>big</th>\n",
       "      <th>small</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>cat</th>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    col_1                  col_2                 \n",
       "        A          B           A          B      \n",
       "      big small  big small   big small  big small\n",
       "cat   1.0   NaN  1.0   NaN   1.0   NaN  1.0   NaN\n",
       "dog   NaN   1.0  NaN   1.0   NaN   1.0  NaN   1.0"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.unstack([0,2])\r\n",
    "#这里转化了第零层col_1和col_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "类似于`pivot`中的唯一性要求，在`unstack`中必须保证$\\color{red}{被转为列索引的行索引层}$和$\\color{red}{被保留的行索引层}$构成的组合是唯一的，例如把前两个列索引改成相同的破坏唯一性，那么就会报错："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>col_1</th>\n",
       "      <th>col_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">A</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">cat</th>\n",
       "      <th>big</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>big</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">B</th>\n",
       "      <th>cat</th>\n",
       "      <th>big</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <th>small</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             col_1  col_2\n",
       "A cat big      1.0    1.0\n",
       "      big      1.0    1.0\n",
       "B cat big      1.0    1.0\n",
       "  dog small    1.0    1.0"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_index = df.index.to_list()\n",
    "my_index[1] = my_index[0]\n",
    "df.index = pd.Index(my_index)\n",
    "df\n",
    "\n",
    "#这里出现了两个big，转换的时候就不知道column对应哪个big了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ValueError('Index contains duplicate entries, cannot reshape')"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "try:\n",
    "    df.unstack()\n",
    "except Exception as e:\n",
    "    Err_Msg = e\n",
    "Err_Msg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "看一看官方文档`df.unstack?`\n",
    "\n",
    "介绍：\n",
    "```\n",
    "Pivot a level of the (necessarily hierarchical) index labels.\n",
    "\n",
    "Returns a DataFrame having a new level of column labels whose inner-most level\n",
    "consists of the pivoted index labels.\n",
    "对最内层的index转化\n",
    "If the index is not a MultiIndex, the output will be a Series\n",
    "(the analogue of stack when the columns are not a MultiIndex).\n",
    "```\n",
    "功能：\n",
    "```\n",
    "Parameters\n",
    "----------\n",
    "level : int, str, or list of these, default -1 (last level)\n",
    "    Level(s) of index to unstack, can pass level name.\n",
    "fill_value : int, str or dict\n",
    "    Replace NaN with this value if the unstack produces missing values.\n",
    "    填充不存在的值\n",
    "```\n",
    "一些例子：\n",
    "```\n",
    "\n",
    "Examples\n",
    "--------\n",
    ">>> index = pd.MultiIndex.from_tuples([('one', 'a'), ('one', 'b'),\n",
    "...                                    ('two', 'a'), ('two', 'b')])\n",
    ">>> s = pd.Series(np.arange(1.0, 5.0), index=index)\n",
    ">>> s\n",
    "one  a   1.0\n",
    "     b   2.0\n",
    "two  a   3.0\n",
    "     b   4.0\n",
    "dtype: float64\n",
    "从最后一个开始(a,b)\n",
    ">>> s.unstack(level=-1)\n",
    "     a   b\n",
    "one  1.0  2.0\n",
    "two  3.0  4.0\n",
    "\n",
    ">>> s.unstack(level=0)\n",
    "   one  two\n",
    "a  1.0   3.0\n",
    "b  2.0   4.0\n",
    "\n",
    ">>> df = s.unstack(level=0)\n",
    "两次的unstack，负负得正，整回去了\n",
    ">>> df.unstack()\n",
    "one  a  1.0\n",
    "     b  2.0\n",
    "two  a  3.0\n",
    "     b  4.0\n",
    "dtype: float64\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "与`unstack`相反，`stack`的作用就是把列索引的层压入行索引，其用法完全类似。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">A</th>\n",
       "      <th colspan=\"2\" halign=\"left\">B</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>cat</th>\n",
       "      <th>dog</th>\n",
       "      <th>cat</th>\n",
       "      <th>dog</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>big</th>\n",
       "      <th>small</th>\n",
       "      <th>big</th>\n",
       "      <th>small</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>index_1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>index_2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           A          B      \n",
       "         cat   dog  cat   dog\n",
       "         big small  big small\n",
       "index_1  1.0   1.0  1.0   1.0\n",
       "index_2  1.0   1.0  1.0   1.0"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.ones((4,2)),\n",
    "                  index = pd.Index([('A', 'cat', 'big'),\n",
    "                                    ('A', 'dog', 'small'),\n",
    "                                    ('B', 'cat', 'big'),\n",
    "                                    ('B', 'dog', 'small')]),\n",
    "                  columns=['index_1', 'index_2']).T\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">A</th>\n",
       "      <th colspan=\"2\" halign=\"left\">B</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>cat</th>\n",
       "      <th>dog</th>\n",
       "      <th>cat</th>\n",
       "      <th>dog</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">index_1</th>\n",
       "      <th>big</th>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>small</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">index_2</th>\n",
       "      <th>big</th>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>small</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 A         B     \n",
       "               cat  dog  cat  dog\n",
       "index_1 big    1.0  NaN  1.0  NaN\n",
       "        small  NaN  1.0  NaN  1.0\n",
       "index_2 big    1.0  NaN  1.0  NaN\n",
       "        small  NaN  1.0  NaN  1.0"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.stack()\r\n",
    "#默认转换的是最内层也就是大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">index_1</th>\n",
       "      <th>cat</th>\n",
       "      <th>big</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <th>small</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">index_2</th>\n",
       "      <th>cat</th>\n",
       "      <th>big</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dog</th>\n",
       "      <th>small</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     A    B\n",
       "index_1 cat big    1.0  1.0\n",
       "        dog small  1.0  1.0\n",
       "index_2 cat big    1.0  1.0\n",
       "        dog small  1.0  1.0"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.stack([1, 2])\r\n",
    "#转换了第一层动物种类和第二次大小"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### 2. 聚合与变形的关系\n",
    "\n",
    "在上面介绍的所有函数中，除了带有聚合效果的`pivot_table`以外，所有的函数在变形前后并不会带来`values`个数的改变，只是这些值在呈现的形式上发生了变化。在上一章讨论的分组聚合操作，由于生成了新的行列索引，因此必然也属于某种特殊的变形操作，但由于聚合之后把原来的多个值变为了一个值，因此`values`的个数产生了变化，这也是分组聚合与变形函数的最大区别。\n",
    "\n",
    "## 三、其他变形函数\n",
    "\n",
    "### 1. crosstab\n",
    "\n",
    "`crosstab`并不是一个值得推荐使用的函数，因为它能实现的所有功能`pivot_table`都能完成，并且速度更快。在默认状态下，`crosstab`可以统计元素组合出现的频数，即`count`操作。例如统计`learn_pandas`数据集中学校和转系情况对应的频数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Transfer</th>\n",
       "      <th>N</th>\n",
       "      <th>Y</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Fudan University</th>\n",
       "      <td>38</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Peking University</th>\n",
       "      <td>28</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai Jiao Tong University</th>\n",
       "      <td>53</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tsinghua University</th>\n",
       "      <td>62</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Transfer                        N  Y\n",
       "School                              \n",
       "Fudan University               38  1\n",
       "Peking University              28  2\n",
       "Shanghai Jiao Tong University  53  0\n",
       "Tsinghua University            62  4"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('data/learn_pandas.csv')\n",
    "pd.crosstab(index = df.School, columns = df.Transfer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "这等价于如下`crosstab`的如下写法，这里的`aggfunc`即聚合参数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Transfer</th>\n",
       "      <th>N</th>\n",
       "      <th>Y</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Fudan University</th>\n",
       "      <td>38.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Peking University</th>\n",
       "      <td>28.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai Jiao Tong University</th>\n",
       "      <td>53.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tsinghua University</th>\n",
       "      <td>62.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Transfer                          N    Y\n",
       "School                                  \n",
       "Fudan University               38.0  1.0\n",
       "Peking University              28.0  2.0\n",
       "Shanghai Jiao Tong University  53.0  NaN\n",
       "Tsinghua University            62.0  4.0"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(index = df.School, columns = df.Transfer, values = [0]*df.shape[0], aggfunc = 'count')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "同样，可以利用`pivot_table`进行等价操作，由于这里统计的是组合的频数，因此`values`参数无论传入哪一个列都不会影响最后的结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Transfer</th>\n",
       "      <th>N</th>\n",
       "      <th>Y</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Fudan University</th>\n",
       "      <td>38.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Peking University</th>\n",
       "      <td>28.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai Jiao Tong University</th>\n",
       "      <td>53.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tsinghua University</th>\n",
       "      <td>62.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Transfer                          N    Y\n",
       "School                                  \n",
       "Fudan University               38.0  1.0\n",
       "Peking University              28.0  2.0\n",
       "Shanghai Jiao Tong University  53.0  NaN\n",
       "Tsinghua University            62.0  4.0"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(index = 'School',\n",
    "               columns = 'Transfer',\n",
    "               values = 'Name',\n",
    "               aggfunc = 'count')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "从上面可以看出这两个函数的区别在于，`crosstab`的对应位置传入的是具体的序列，而`pivot_table`传入的是被调用表对应的名字，若传入序列对应的值则会报错。\n",
    "\n",
    "除了默认状态下的`count`统计，所有的聚合字符串和返回标量的自定义函数都是可用的，例如统计对应组合的身高均值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Transfer</th>\n",
       "      <th>N</th>\n",
       "      <th>Y</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Fudan University</th>\n",
       "      <td>162.043750</td>\n",
       "      <td>177.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Peking University</th>\n",
       "      <td>163.429630</td>\n",
       "      <td>162.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai Jiao Tong University</th>\n",
       "      <td>163.953846</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tsinghua University</th>\n",
       "      <td>163.253571</td>\n",
       "      <td>164.55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Transfer                                N       Y\n",
       "School                                           \n",
       "Fudan University               162.043750  177.20\n",
       "Peking University              163.429630  162.40\n",
       "Shanghai Jiao Tong University  163.953846     NaN\n",
       "Tsinghua University            163.253571  164.55"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(index = df.School, columns = df.Transfer, values = df.Height, aggfunc = 'mean')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "#### 【练一练】\n",
    "前面提到了`crosstab`的性能劣于`pivot_table`，请选用多个聚合方法进行验证。\n",
    "\n",
    "以运行时间来验证：大多数情况下crosstab的max、min比pivot_tab快,有的函数运行不了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 12 ms, sys: 0 ns, total: 12 ms\n",
      "Wall time: 26.2 ms\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Transfer</th>\n",
       "      <th>N</th>\n",
       "      <th>Y</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Fudan University</th>\n",
       "      <td>177.3</td>\n",
       "      <td>177.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Peking University</th>\n",
       "      <td>185.3</td>\n",
       "      <td>162.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai Jiao Tong University</th>\n",
       "      <td>188.9</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tsinghua University</th>\n",
       "      <td>193.9</td>\n",
       "      <td>170.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Transfer                           N      Y\n",
       "School                                     \n",
       "Fudan University               177.3  177.2\n",
       "Peking University              185.3  162.4\n",
       "Shanghai Jiao Tong University  188.9    NaN\n",
       "Tsinghua University            193.9  170.7"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\r\n",
    "pd.crosstab(index = df.School, columns = df.Transfer, values = df.Height, aggfunc = 'max')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 8 ms, sys: 8 ms, total: 16 ms\n",
      "Wall time: 13.1 ms\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Transfer</th>\n",
       "      <th>N</th>\n",
       "      <th>Y</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Fudan University</th>\n",
       "      <td>Yanquan Wang</td>\n",
       "      <td>Chengpeng Qian</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Peking University</th>\n",
       "      <td>Xiaopeng Qin</td>\n",
       "      <td>Xiaojuan Qin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai Jiao Tong University</th>\n",
       "      <td>Yanpeng Lv</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tsinghua University</th>\n",
       "      <td>Yanquan Lv</td>\n",
       "      <td>Yanli Qin</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Transfer                                  N               Y\n",
       "School                                                     \n",
       "Fudan University               Yanquan Wang  Chengpeng Qian\n",
       "Peking University              Xiaopeng Qin    Xiaojuan Qin\n",
       "Shanghai Jiao Tong University    Yanpeng Lv             NaN\n",
       "Tsinghua University              Yanquan Lv       Yanli Qin"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\r\n",
    "df.pivot_table(index = 'School',\r\n",
    "               columns = 'Transfer',\r\n",
    "               values = 'Name',\r\n",
    "               aggfunc = 'max')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 8 ms, sys: 4 ms, total: 12 ms\n",
      "Wall time: 56.5 ms\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Transfer</th>\n",
       "      <th>N</th>\n",
       "      <th>Y</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Fudan University</th>\n",
       "      <td>147.3</td>\n",
       "      <td>177.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Peking University</th>\n",
       "      <td>148.7</td>\n",
       "      <td>162.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai Jiao Tong University</th>\n",
       "      <td>145.4</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tsinghua University</th>\n",
       "      <td>150.5</td>\n",
       "      <td>157.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Transfer                           N      Y\n",
       "School                                     \n",
       "Fudan University               147.3  177.2\n",
       "Peking University              148.7  162.4\n",
       "Shanghai Jiao Tong University  145.4    NaN\n",
       "Tsinghua University            150.5  157.4"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\r\n",
    "pd.crosstab(index = df.School, columns = df.Transfer, values = df.Height, aggfunc = 'min')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 16 ms, sys: 8 ms, total: 24 ms\n",
      "Wall time: 18.3 ms\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Transfer</th>\n",
       "      <th>N</th>\n",
       "      <th>Y</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>School</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Fudan University</th>\n",
       "      <td>Changfeng Lv</td>\n",
       "      <td>Chengpeng Qian</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Peking University</th>\n",
       "      <td>Changjuan You</td>\n",
       "      <td>Xiaojuan Chu</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai Jiao Tong University</th>\n",
       "      <td>Changli Qin</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tsinghua University</th>\n",
       "      <td>Changjuan Xu</td>\n",
       "      <td>Chengpeng You</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Transfer                                   N               Y\n",
       "School                                                      \n",
       "Fudan University                Changfeng Lv  Chengpeng Qian\n",
       "Peking University              Changjuan You    Xiaojuan Chu\n",
       "Shanghai Jiao Tong University    Changli Qin             NaN\n",
       "Tsinghua University             Changjuan Xu   Chengpeng You"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\r\n",
    "df.pivot_table(index = 'School',\r\n",
    "               columns = 'Transfer',\r\n",
    "               values = 'Name',\r\n",
    "               aggfunc = 'min')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "\n",
    "#### 【END】\n",
    "### 2. explode\n",
    "\n",
    "`explode`参数能够对某一列的元素进行纵向的展开，被展开的单元格必须存储`list, tuple, Series, np.ndarray`中的一种类型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[1, 2]</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>my_str</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>{1, 2}</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0    3\n",
       "1    4\n",
       "dtype: int64</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            A  B\n",
       "0                      [1, 2]  1\n",
       "1                      my_str  1\n",
       "2                      {1, 2}  1\n",
       "3  0    3\n",
       "1    4\n",
       "dtype: int64  1"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_ex = pd.DataFrame({'A': [[1, 2], 'my_str', {1, 2}, pd.Series([3, 4])],\n",
    "                      'B': 1})\n",
    "df_ex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>my_str</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>{1, 2}</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        A  B\n",
       "0       1  1\n",
       "0       2  1\n",
       "1  my_str  1\n",
       "2  {1, 2}  1\n",
       "3       3  1\n",
       "3       4  1"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_ex.explode('A')\r\n",
    "#展开的结果成为了多行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### 3. get_dummies\n",
    "\n",
    "`get_dummies`是用于特征构建的重要函数之一，其作用是把类别特征转为指示变量。例如，对年级一列转为指示变量，属于某一个年级的对应列标记为1，否则为0，也就是返回和原DataFrame一样大小的布尔值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Freshman</th>\n",
       "      <th>Junior</th>\n",
       "      <th>Senior</th>\n",
       "      <th>Sophomore</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Freshman  Junior  Senior  Sophomore\n",
       "0         1       0       0          0\n",
       "1         1       0       0          0\n",
       "2         0       0       1          0\n",
       "3         0       0       0          1\n",
       "4         0       0       0          1"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.get_dummies(df.Grade).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "看一看官方文档`pd.get_dummies?`：Convert categorical variable into dummy/indicator variables.\n",
    "首先是输入输出\n",
    "```\n",
    "pd.get_dummies(\n",
    "    data,\n",
    "    prefix=None,\n",
    "    prefix_sep='_',\n",
    "    dummy_na=False,\n",
    "    columns=None,\n",
    "    sparse=False,\n",
    "    drop_first=False,\n",
    "    dtype=None,\n",
    ") -> 'DataFrame'\n",
    "```\n",
    "具体功能：\n",
    "```\n",
    "Parameters\n",
    "----------\n",
    "data : array-like, Series, or DataFrame\n",
    "    Data of which to get dummy indicators.\n",
    "    数据，三种都行\n",
    "prefix : str, list of str, or dict of str, default None\n",
    "    String to append DataFrame column names.\n",
    "    Pass a list with length equal to the number of columns\n",
    "    when calling get_dummies on a DataFrame. Alternatively, `prefix`\n",
    "    can be a dictionary mapping column names to prefixes.\n",
    "    添加在DF上的列名\n",
    "prefix_sep : str, default '_'\n",
    "    If appending prefix, separator/delimiter to use. Or pass a\n",
    "    list or dictionary as with `prefix`.\n",
    "    分隔符，默认是下划线\n",
    "dummy_na : bool, default False\n",
    "    Add a column to indicate NaNs, if False NaNs are ignored.\n",
    "    专门增加一列表示NaN\n",
    "columns : list-like, default None\n",
    "    Column names in the DataFrame to be encoded.\n",
    "    If `columns` is None then all the columns with\n",
    "    `object` or `category` dtype will be converted.\n",
    "sparse : bool, default False\n",
    "    Whether the dummy-encoded columns should be backed by\n",
    "    a :class:`SparseArray` (True) or a regular NumPy array (False).\n",
    "drop_first : bool, default False\n",
    "    Whether to get k-1 dummies out of k categorical levels by removing the\n",
    "    first level.\n",
    "    要不要把第一层去掉\n",
    "dtype : dtype, default np.uint8\n",
    "    Data type for new columns. Only a single dtype is allowed.\n",
    "\n",
    "```\n",
    "\n",
    "一些例子：\n",
    "```\n",
    "\n",
    "Examples\n",
    "--------\n",
    ">>> s = pd.Series(list('abca'))\n",
    "\n",
    ">>> pd.get_dummies(s)\n",
    "   a  b  c\n",
    "0  1  0  0\n",
    "1  0  1  0\n",
    "2  0  0  1\n",
    "3  1  0  0\n",
    "\n",
    ">>> s1 = ['a', 'b', np.nan]\n",
    "\n",
    ">>> pd.get_dummies(s1)\n",
    "   a  b\n",
    "0  1  0\n",
    "1  0  1\n",
    "2  0  0\n",
    "\n",
    "单独增加一列表示NaN\n",
    ">>> pd.get_dummies(s1, dummy_na=True)\n",
    "   a  b  NaN\n",
    "0  1  0    0\n",
    "1  0  1    0\n",
    "2  0  0    1\n",
    "\n",
    ">>> df = pd.DataFrame({'A': ['a', 'b', 'a'], 'B': ['b', 'a', 'c'],\n",
    "...                    'C': [1, 2, 3]})\n",
    "对a和b增加了col1和col2\n",
    ">>> pd.get_dummies(df, prefix=['col1', 'col2'])\n",
    "   C  col1_a  col1_b  col2_a  col2_b  col2_c\n",
    "0  1       1       0       0       1       0\n",
    "1  2       0       1       1       0       0\n",
    "2  3       1       0       0       0       1\n",
    "\n",
    ">>> pd.get_dummies(pd.Series(list('abcaa')))\n",
    "   a  b  c\n",
    "0  1  0  0\n",
    "1  0  1  0\n",
    "2  0  0  1\n",
    "3  1  0  0\n",
    "4  1  0  0\n",
    "\n",
    "把第一个a丢掉了\n",
    ">>> pd.get_dummies(pd.Series(list('abcaa')), drop_first=True)\n",
    "   b  c\n",
    "0  0  0\n",
    "1  1  0\n",
    "2  0  1\n",
    "3  0  0\n",
    "4  0  0\n",
    "数据类型选择\n",
    ">>> pd.get_dummies(pd.Series(list('abc')), dtype=float)\n",
    "     a    b    c\n",
    "0  1.0  0.0  0.0\n",
    "1  0.0  1.0  0.0\n",
    "2  0.0  0.0  1.0\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 四、练习\n",
    "### Ex1：美国非法药物数据集\n",
    "\n",
    "现有一份关于美国非法药物的数据集，其中`SubstanceName, DrugReports`分别指药物名称和报告数量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>YYYY</th>\n",
       "      <th>State</th>\n",
       "      <th>COUNTY</th>\n",
       "      <th>SubstanceName</th>\n",
       "      <th>DrugReports</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2011</td>\n",
       "      <td>KY</td>\n",
       "      <td>ADAIR</td>\n",
       "      <td>Buprenorphine</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2012</td>\n",
       "      <td>KY</td>\n",
       "      <td>ADAIR</td>\n",
       "      <td>Buprenorphine</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2013</td>\n",
       "      <td>KY</td>\n",
       "      <td>ADAIR</td>\n",
       "      <td>Buprenorphine</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   YYYY State COUNTY  SubstanceName  DrugReports\n",
       "0  2011    KY  ADAIR  Buprenorphine            3\n",
       "1  2012    KY  ADAIR  Buprenorphine            5\n",
       "2  2013    KY  ADAIR  Buprenorphine            4"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('data/drugs.csv').sort_values(['State','COUNTY','SubstanceName'],ignore_index=True)\n",
    "df.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "1. 将数据转为如下的形式：\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/cc6d4a52b81a4567b36d8be7bd2b48f28fc4fc1eff774b58be6c8a4c4b144c38)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "思路：把原来是行标签的全部pivot成列标签，也就是年份变形成列索引\n",
    "\n",
    "然后生成的表格YYYY在最上一排，不完全一样的，需要重新排列index\n",
    "\n",
    "用reset_index后把所有的列标签都弄到了最上一层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>YYYY</th>\n",
       "      <th>State</th>\n",
       "      <th>COUNTY</th>\n",
       "      <th>SubstanceName</th>\n",
       "      <th>2010</th>\n",
       "      <th>2011</th>\n",
       "      <th>2012</th>\n",
       "      <th>2013</th>\n",
       "      <th>2014</th>\n",
       "      <th>2015</th>\n",
       "      <th>2016</th>\n",
       "      <th>2017</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>KY</td>\n",
       "      <td>ADAIR</td>\n",
       "      <td>Buprenorphine</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>KY</td>\n",
       "      <td>ADAIR</td>\n",
       "      <td>Codeine</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>KY</td>\n",
       "      <td>ADAIR</td>\n",
       "      <td>Fentanyl</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "YYYY State COUNTY  SubstanceName  2010  2011  2012  2013  2014  2015  2016  \\\n",
       "0       KY  ADAIR  Buprenorphine   NaN   3.0   5.0   4.0  27.0   5.0   7.0   \n",
       "1       KY  ADAIR        Codeine   NaN   NaN   1.0   NaN   NaN   NaN   NaN   \n",
       "2       KY  ADAIR       Fentanyl   NaN   NaN   1.0   NaN   NaN   NaN   NaN   \n",
       "\n",
       "YYYY  2017  \n",
       "0     10.0  \n",
       "1      1.0  \n",
       "2      NaN  "
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = df.pivot_table(index=['State','COUNTY','SubstanceName'], columns='YYYY', values='DrugReports').head(3)\r\n",
    "#['2010','2011','2012','2013','2014','2015','2016','2017']\r\n",
    "df1 = df1.reset_index(['State','COUNTY','SubstanceName'])\r\n",
    "df1\r\n",
    "\r\n",
    "#参考答案：这里我忘了把YYYY去掉，可以直接替换隐藏\r\n",
    "#df.pivot(index=['State','COUNTY','SubstanceName'], columns='YYYY', values='DrugReports').reset_index().rename_axis(columns={'YYYY':''})\r\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "2. 将第1问中的结果恢复为原表。\n",
    "\n",
    "按照第一问的思路反过来就好\n",
    "\n",
    "使用melt，列索引还是原来的三个，要表变成长表的是年份，名字是YYYY，用到的值是DrugReport\n",
    "\n",
    "注意到之前添加上了很多NaN，所以加上.dropna\n",
    "\n",
    "完成后发现几个问题：列顺序不一样，and数据精度不一样\n",
    "\n",
    "先用astype换了数据类型\n",
    "\n",
    "然后直接大力出奇迹交换列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/ipykernel_launcher.py:4: FutureWarning: This dataframe has a column name that matches the 'value_name' column name of the resultiing Dataframe. In the future this will raise an error, please set the 'value_name' parameter of DataFrame.melt to a unique name.\n",
      "  after removing the cwd from sys.path.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>YYYY</th>\n",
       "      <th>State</th>\n",
       "      <th>COUNTY</th>\n",
       "      <th>SubstanceName</th>\n",
       "      <th>DrugReports</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>DrugReports</td>\n",
       "      <td>KY</td>\n",
       "      <td>ADAIR</td>\n",
       "      <td>Buprenorphine</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>DrugReports</td>\n",
       "      <td>KY</td>\n",
       "      <td>ADAIR</td>\n",
       "      <td>Buprenorphine</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>DrugReports</td>\n",
       "      <td>KY</td>\n",
       "      <td>ADAIR</td>\n",
       "      <td>Codeine</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>DrugReports</td>\n",
       "      <td>KY</td>\n",
       "      <td>ADAIR</td>\n",
       "      <td>Fentanyl</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>DrugReports</td>\n",
       "      <td>KY</td>\n",
       "      <td>ADAIR</td>\n",
       "      <td>Buprenorphine</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          YYYY State COUNTY  SubstanceName  DrugReports\n",
       "0  DrugReports    KY  ADAIR  Buprenorphine            3\n",
       "1  DrugReports    KY  ADAIR  Buprenorphine            5\n",
       "2  DrugReports    KY  ADAIR        Codeine            1\n",
       "3  DrugReports    KY  ADAIR       Fentanyl            1\n",
       "4  DrugReports    KY  ADAIR  Buprenorphine            4"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = df1.melt(id_vars = ['State','COUNTY','SubstanceName'],\n",
    "                      value_vars = df1.columns[4:11],\n",
    "                      var_name = 'YYYY',\n",
    "                      value_name = 'DrugReports').dropna()\n",
    "df1['DrugReports'] = df1['DrugReports'].astype('int')\n",
    "df1 = df1[['YYYY','State','COUNTY','SubstanceName','DrugReports']]\n",
    "#df1[['State','COUNTY','SubstanceName','YYYY']] = df1[['YYYY','State','COUNTY','SubstanceName']]\n",
    "df1.head()\n",
    "\n",
    "#参考答案思路更好一点\n",
    "'''\n",
    "res_melted = res.melt(id_vars = ['State','COUNTY','SubstanceName'],\n",
    "                      value_vars = res.columns[-8:],\n",
    "                      var_name = 'YYYY',\n",
    "                      value_name = 'DrugReports').dropna(\n",
    "                      subset=['DrugReports'])\n",
    "res_melted = res_melted[df.columns].sort_values(['State','COUNTY','SubstanceName'],ignore_index=True).astype({'YYYY':'int64', 'DrugReports':'int64'})\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "3. 按`State`分别统计每年的报告数量总和，其中`State, YYYY`分别为列索引和行索引，要求分别使用`pivot_table`函数与`groupby+unstack`两种不同的策略实现，并体会它们之间的联系。\n",
    "\n",
    "pivot需要`pivot(index,column,value,addfunc)`\n",
    "\n",
    "groupby需要`df.groupby(分组依据)[数据来源].使用操作`之后形成的表再用unstack把第一层KY，OH等等变成列索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>State</th>\n",
       "      <th>KY</th>\n",
       "      <th>OH</th>\n",
       "      <th>PA</th>\n",
       "      <th>VA</th>\n",
       "      <th>WV</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>YYYY</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2010</th>\n",
       "      <td>10453</td>\n",
       "      <td>19707</td>\n",
       "      <td>19814</td>\n",
       "      <td>8685</td>\n",
       "      <td>2890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011</th>\n",
       "      <td>10289</td>\n",
       "      <td>20330</td>\n",
       "      <td>19987</td>\n",
       "      <td>6749</td>\n",
       "      <td>3271</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012</th>\n",
       "      <td>10722</td>\n",
       "      <td>23145</td>\n",
       "      <td>19959</td>\n",
       "      <td>7831</td>\n",
       "      <td>3376</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "State     KY     OH     PA    VA    WV\n",
       "YYYY                                  \n",
       "2010   10453  19707  19814  8685  2890\n",
       "2011   10289  20330  19987  6749  3271\n",
       "2012   10722  23145  19959  7831  3376"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(index='YYYY', columns='State', values='DrugReports', aggfunc='sum').head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>State</th>\n",
       "      <th>KY</th>\n",
       "      <th>OH</th>\n",
       "      <th>PA</th>\n",
       "      <th>VA</th>\n",
       "      <th>WV</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>YYYY</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2010</th>\n",
       "      <td>10453</td>\n",
       "      <td>19707</td>\n",
       "      <td>19814</td>\n",
       "      <td>8685</td>\n",
       "      <td>2890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011</th>\n",
       "      <td>10289</td>\n",
       "      <td>20330</td>\n",
       "      <td>19987</td>\n",
       "      <td>6749</td>\n",
       "      <td>3271</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012</th>\n",
       "      <td>10722</td>\n",
       "      <td>23145</td>\n",
       "      <td>19959</td>\n",
       "      <td>7831</td>\n",
       "      <td>3376</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "State     KY     OH     PA    VA    WV\n",
       "YYYY                                  \n",
       "2010   10453  19707  19814  8685  2890\n",
       "2011   10289  20330  19987  6749  3271\n",
       "2012   10722  23145  19959  7831  3376"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3 = df.groupby(['State', 'YYYY'])['DrugReports'].sum()\r\n",
    "#df3\r\n",
    "df3.unstack(0).head(3)\r\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "\n",
    "\n",
    "\n",
    "### Ex2：特殊的wide_to_long方法\n",
    "\n",
    "从功能上看，`melt`方法应当属于`wide_to_long`的一种特殊情况，即`stubnames`只有一类。请使用`wide_to_long`生成`melt`一节中的`df_melted`。（提示：对列名增加适当的前缀）\n",
    "\n",
    "首先看一下melt的效果是把Chinese和Math转换成了行标签\n",
    "\n",
    "wide_to_long想实现相同效果:首先把列名加上前缀，然后就可以拆成stubnames，直接把前缀重命名为分数\n",
    "\n",
    "得到结果后会发现新的前缀在最上层，用reset使他们在同一层\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Name</th>\n",
       "      <th>Chinese</th>\n",
       "      <th>Math</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>80</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>90</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Class       Name  Chinese  Math\n",
       "0      1  San Zhang       80    80\n",
       "1      2      Si Li       90    75"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'Class':[1,2],\n",
    "                   'Name':['San Zhang', 'Si Li'],\n",
    "                   'Chinese':[80, 90],\n",
    "                   'Math':[80, 75]})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Name</th>\n",
       "      <th>Subject</th>\n",
       "      <th>Grade</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Math</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Math</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Class       Name  Subject  Grade\n",
       "0      1  San Zhang  Chinese     80\n",
       "1      2      Si Li  Chinese     90\n",
       "2      1  San Zhang     Math     80\n",
       "3      2      Si Li     Math     75"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_melted = df.melt(id_vars = ['Class', 'Name'],\r\n",
    "                    value_vars = ['Chinese', 'Math'],\r\n",
    "                    var_name = 'Subject',\r\n",
    "                    value_name = 'Grade')\r\n",
    "df_melted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Class</th>\n",
       "      <th>Name</th>\n",
       "      <th>Subject</th>\n",
       "      <th>Grade</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>San Zhang</td>\n",
       "      <td>Math</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Chinese</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>Si Li</td>\n",
       "      <td>Math</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Class       Name  Subject  Grade\n",
       "0      1  San Zhang  Chinese     80\n",
       "1      1  San Zhang     Math     80\n",
       "2      2      Si Li  Chinese     90\n",
       "3      2      Si Li     Math     75"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.rename(columns={'Chinese':'Grade/Chinese', 'Math':'Grade/Math'})\r\n",
    "pd.wide_to_long(df,\r\n",
    "                stubnames=['Grade'],\r\n",
    "                i = ['Class', 'Name'],\r\n",
    "                j='Subject',\r\n",
    "                sep='/',\r\n",
    "                suffix='.+').reset_index()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PaddlePaddle 2.0.0b0 (Python 3.5)",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
